﻿using System.Collections.Generic;
using SuddenDeath.Domain.Services.FileManager.elFinder.Responses;
using SuddenDeath.Domain.Services.FileManager.elFinder.Utils;

namespace SuddenDeath.Domain.Services.FileManager.elFinder.Commands
{
    public class Rm : ICommand
    {
        public string Current { get; set; }

        public string Name { get; set; }

        public IEnumerable<string> ToDelete { get; set; }

        public Rm(string current, string toDelete)
        {
            Current = current;

            var arr = toDelete.Split(",".ToCharArray());

            ToDelete = arr;
        }

        #region ICommand Members       

        public Response Execute()
        {
            var path = DirectoryUtils.GetFilePathByHash(Current, Configuration.Configuration.RootPath);

            foreach (var name in ToDelete)
            {
                var toDelete = DirectoryUtils.GetFilePathByHash(name, Configuration.Configuration.RootPath);

                try
                {
                    System.IO.File.Delete(toDelete);
                }
                catch
                {
                    var dir = new System.IO.DirectoryInfo(toDelete);
                    dir.DeleteWithFiles();
                }
            }

            var folder = new Folder(path);

            var response = new DirectoryResponse { cwd = folder.Cwd, cdc = folder.Contents, tree = folder.Tree };

            return response;
        }

        #endregion
    }
}
